package org.wishwork.api.order.upload;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wishwork.api.Api;
import org.wishwork.api.order.upload.request.Item;
import org.wishwork.api.order.upload.request.OrderUploadRequest;
import org.wishwork.context.Context;
import org.wishwork.helper.HttpHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 订单上传
 * @author: WangYX
 * @create: 2022-04-13 15:47
 * @Version: 1.0.0
 **/

public class OrderUploadApi extends Api<List<OrderUploadResponse>, List<OrderUploadRequest>> {

    private static final Logger log = LoggerFactory.getLogger(OrderUploadApi.class);

    private Context context;

    public OrderUploadApi(Context context) {
        this.context = context;
    }


    @Override
    protected String query(List<OrderUploadRequest> orderUploadRequests) throws Exception {
        String url = "/open/jushuitan/orders/upload";
        List<Map<String, Object>> list = new ArrayList<>();
        for (OrderUploadRequest orderUploadRequest : orderUploadRequests) {
            Map<String, Object> params = new HashMap<>();
            params.put("shop_id", orderUploadRequest.getShopId());
            params.put("so_id", orderUploadRequest.getSoId());
            params.put("order_date", orderUploadRequest.getOrderDate());
            params.put("shop_status", orderUploadRequest.getShopStatus());
            params.put("shop_buyer_id", orderUploadRequest.getShopBuyerId());
            params.put("receiver_state", orderUploadRequest.getReceiverState());
            params.put("receiver_city", orderUploadRequest.getReceiverCity());
            params.put("receiver_district", orderUploadRequest.getReceiverDistrict());
            params.put("receiver_address", orderUploadRequest.getReceiverAddress());
            params.put("receiver_name", orderUploadRequest.getReceiverName());
            params.put("receiver_phone", orderUploadRequest.getReceiverPhone());
            if (StrUtil.isNotEmpty(orderUploadRequest.getReceiverMobile())) {
                params.put("receiver_mobile", orderUploadRequest.getReceiverMobile());
            }
            params.put("pay_amount", orderUploadRequest.getPayAmount());
            params.put("freight", orderUploadRequest.getFreight());
            if (StrUtil.isNotEmpty(orderUploadRequest.getBuyerMessage())) {
                params.put("buyer_message", orderUploadRequest.getBuyerMessage());
            }
            if (StrUtil.isNotEmpty(orderUploadRequest.getRemark())) {
                params.put("remark", orderUploadRequest.getRemark());
            }
            if (ObjectUtil.isNotEmpty(orderUploadRequest.getIsCod())) {
                params.put("is_cod", orderUploadRequest.getIsCod());
            }
            params.put("shop_modified", orderUploadRequest.getShopModified());
            if (StrUtil.isNotEmpty(orderUploadRequest.getLId())) {
                params.put("l_id", orderUploadRequest.getLId());
            }
            if (StrUtil.isNotEmpty(orderUploadRequest.getLogisticsCompany())) {
                params.put("logistics_company", orderUploadRequest.getLogisticsCompany());
            }
            if (StrUtil.isNotEmpty(orderUploadRequest.getQuestionDesc())) {
                params.put("question_desc", orderUploadRequest.getQuestionDesc());
            }
            if (orderUploadRequest.getSellerFlag() != null) {
                params.put("seller_flag", orderUploadRequest.getSellerFlag());
            }
            List<Map<String, Object>> items = new ArrayList<>();
            for (Item item : orderUploadRequest.getItems()) {
                Map<String, Object> map = new HashMap<>();
                map.put("sku_id", item.getSkuId());
                map.put("shop_sku_id", item.getShopSkuId());
                if (StrUtil.isNotEmpty(item.getIId())) {
                    map.put("i_id", item.getIId());
                }
                if (StrUtil.isNotEmpty(item.getPic())) {
                    map.put("pic", item.getPic());
                }
                if (StrUtil.isNotEmpty(item.getPropertiesValue())) {
                    map.put("properties_value", item.getPropertiesValue());
                }
                map.put("amount", item.getAmount());
                map.put("base_price", item.getBasePrice());
                map.put("qty", item.getQty());
                map.put("name", item.getName());
                if (StrUtil.isNotEmpty(item.getRefundStatus())) {
                    map.put("refund_status", item.getRefundStatus());
                }
                map.put("outer_oi_id", item.getOuterOiId());
                if (StrUtil.isNotEmpty(item.getRemark())) {
                    map.put("remark", item.getRemark());
                }
                items.add(map);
            }
            params.put("items", items);
            if (ObjectUtil.isNotEmpty(orderUploadRequest.getPay())) {
                Map<String, Object> map = new HashMap<>();
                map.put("outer_pay_id", orderUploadRequest.getPay().getOuterPayId());
                map.put("pay_date", orderUploadRequest.getPay().getPayDate());
                map.put("payment", orderUploadRequest.getPay().getPayment());
                map.put("seller_account", orderUploadRequest.getPay().getSellerAccount());
                map.put("buyer_account", orderUploadRequest.getPay().getBuyerAccount());
                map.put("amount", orderUploadRequest.getPay().getAmount());
                params.put("pay", map);
            }
            if (ObjectUtil.isNotEmpty(orderUploadRequest.getCard())) {
                Map<String, Object> map = new HashMap<>();
                map.put("name", orderUploadRequest.getCard().getName());
                map.put("card_id", orderUploadRequest.getCard().getCardId());
                params.put("card", map);
            }

            if (StrUtil.isNotEmpty(orderUploadRequest.getSendDate())) {
                params.put("send_date", orderUploadRequest.getSendDate());
            }
            if (StrUtil.isNotEmpty(orderUploadRequest.getLabels())) {
                params.put("labels", orderUploadRequest.getLabels());
            }
            if (StrUtil.isNotEmpty(orderUploadRequest.getLcId())) {
                params.put("lc_id", orderUploadRequest.getLcId());
            }
            if (StrUtil.isNotEmpty(orderUploadRequest.getReceiverCountry())) {
                params.put("receiver_country", orderUploadRequest.getReceiverCountry());
            }
            if (StrUtil.isNotEmpty(orderUploadRequest.getReceiverZip())) {
                params.put("receiver_zip", orderUploadRequest.getReceiverZip());
            }
            if (StrUtil.isNotEmpty(orderUploadRequest.getCurrency())) {
                params.put("currency", orderUploadRequest.getCurrency());
            }
            if (StrUtil.isNotEmpty(orderUploadRequest.getInvoiceType())) {
                params.put("invoice_type", orderUploadRequest.getInvoiceType());
            }
            if (StrUtil.isNotEmpty(orderUploadRequest.getInvoiceTitle())) {
                params.put("invoice_title", orderUploadRequest.getInvoiceTitle());
            }
            if (StrUtil.isNotEmpty(orderUploadRequest.getBuyerTaxNo())) {
                params.put("buyer_tax_no", orderUploadRequest.getBuyerTaxNo());
            }
            if (ObjectUtil.isNotEmpty(orderUploadRequest.getInvoice())) {
                Map<String, Object> map = new HashMap<>();
                if (StrUtil.isNotEmpty(orderUploadRequest.getInvoice().getAddress())) {
                    map.put("address", orderUploadRequest.getInvoice().getAddress());
                }
                if (StrUtil.isNotEmpty(orderUploadRequest.getInvoice().getBank())) {
                    map.put("bank", orderUploadRequest.getInvoice().getBank());
                }
                if (StrUtil.isNotEmpty(orderUploadRequest.getInvoice().getPhone())) {
                    map.put("phone", orderUploadRequest.getInvoice().getPhone());
                }
                if (StrUtil.isNotEmpty(orderUploadRequest.getInvoice().getAccount())) {
                    map.put("account", orderUploadRequest.getInvoice().getAccount());
                }
                if (StrUtil.isNotEmpty(orderUploadRequest.getInvoice().getEmail())) {
                    map.put("email", orderUploadRequest.getInvoice().getEmail());
                }
                if (StrUtil.isNotEmpty(orderUploadRequest.getInvoice().getUserName())) {
                    map.put("userName", orderUploadRequest.getInvoice().getUserName());
                }
                if (StrUtil.isNotEmpty(orderUploadRequest.getInvoice().getUserPhone())) {
                    map.put("userPhone", orderUploadRequest.getInvoice().getUserPhone());
                }
                if (StrUtil.isNotEmpty(orderUploadRequest.getInvoice().getUserAddress())) {
                    map.put("userAddress", orderUploadRequest.getInvoice().getUserAddress());
                }
                params.put("invoice", map);
            }
            if (StrUtil.isNotEmpty(orderUploadRequest.getNode())) {
                params.put("node", orderUploadRequest.getNode());
            }
            if (StrUtil.isNotEmpty(orderUploadRequest.getReceiverTown())) {
                params.put("receiver_town", orderUploadRequest.getReceiverTown());
            }
            list.add(params);
        }
        String biz = JSON.toJSONString(list);
        return HttpHelper.sendRequest(biz, url);
    }

    @Override
    protected List<OrderUploadResponse> process(JSONObject data, List<OrderUploadRequest> orderUploadRequests) throws Exception {
        JSONArray datas = data.getJSONArray("datas");
        List<OrderUploadResponse> orderUploadResponses = datas.toJavaList(OrderUploadResponse.class);
        return orderUploadResponses;
    }


}
